package com.twu.biblioteca.commands;

import com.twu.biblioteca.Biblioteca;
import com.twu.biblioteca.Movie;

import java.io.InputStream;
import java.io.PrintStream;
import java.util.Formatter;

public class MovieListCommand extends BibliotecaCommand {
    public static final String LIST_MESSAGE = "We have the following movies in stock: ";

    public MovieListCommand(InputStream input, PrintStream output, Biblioteca biblioteca) {
        super("List Movies", input, output, biblioteca);
    }

    public boolean run() {
        output.println(LIST_MESSAGE);
        output.println(getFormattedBookList());
        return false;
    }

    private String getFormattedBookList() {
        StringBuilder builder = new StringBuilder();
        Formatter formatter = new Formatter(builder);

        builder.append("--------------------------\n");
        formatter.format("%-24s%-5s%-24s%-7s\n", "Title", "Year", "Director", "Rating");
        builder.append("--------------------------\n");

        for (Movie movie : biblioteca.getMovies()) {
            formatter.format("%-24s%-5d%-24s%-7s\n", movie.getTitle(), movie.getYearReleased(), movie.getDirector(), movie.getRating());
        }

        builder.append("--------------------------\n");

        return builder.toString();
    }
}
